package com.banciyuan.bcywebview.biz.write.photoselecotor.ui;
/**
 * @author Aizaz AZ
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.polites.GestureImageView;
import com.banciyuan.bcywebview.utils.file.FileTypeUtil;
import com.banciyuan.bcywebview.utils.http.GifLoader;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.FailReason;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.io.File;
import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;


public class PhotoPreview extends LinearLayout implements OnClickListener {

    private ProgressBar pbLoading;
    private GestureImageView ivContent;
    private GifImageView ivGif;
    private OnClickListener l;

    public PhotoPreview(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.view_photopreview, this, true);
        pbLoading = (ProgressBar) findViewById(R.id.pb_loading_vpp);
        ivContent = (GestureImageView) findViewById(R.id.iv_content_vpp);
        ivGif = (GifImageView) findViewById(R.id.gif_content_vpp);
        ivContent.setOnClickListener(this);
    }

    public PhotoPreview(Context context, AttributeSet attrs, int defStyle) {
        this(context);
    }

    public PhotoPreview(Context context, AttributeSet attrs) {
        this(context);
    }

    public void loadImage(final PhotoModel photoModel) {
        try {
            String path = "file://" + photoModel.getOriginalPath();
            File imgFile = new File(photoModel.getOriginalPath());
            if (StringUtil.notNullEqual(new FileTypeUtil().getFileByFile(imgFile), "gif")) {
                ivGif.setVisibility(VISIBLE);
                ivGif.setImageDrawable(new GifDrawable(imgFile));
            } else {
                ivGif.setVisibility(GONE);
                ImageLoader.getInstance().loadImage(path, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        ivContent.setImageBitmap(loadedImage);
                        pbLoading.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        ivContent.setImageResource(R.drawable.acg_checked);
                        pbLoading.setVisibility(View.GONE);
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        this.l = l;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_content_vpp && l != null)
            l.onClick(ivContent);
    }
}
